// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../libs/bmap-wx.min.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchList: [{
        posImg: '../../images/logo.png',
        posTxt: "南昌大学第二附属医院",
        id: 0,
        latitude: '28.68240846943',
        longitude: '115.90201380553',
      },
      {
        posImg: '../../images/logo.png',
        posTxt: "江西泰康拜博口腔医院",
        id: 1,
        latitude: 28.67836556493,
        longitude: 115.85568607201
      },
      {
        posImg: '../../images/logo.png',
        posTxt: "南昌市洪都中医院",
        id: 2,
        latitude: 28.706244631,
        longitude: 115.872726345
      },
      {
        posImg: '../../images/logo.png',
        posTxt: "江西省人民医院",
        id: 3,
        latitude: 28.705844618263,
        longitude: 115.87316188483
      },
      {
        posImg: '../../images/logo.png',
        posTxt: "江西省儿童医院",
        id: 4,
        latitude: 28.687631674617,
        longitude: 115.89590244399
      },
    ],
    // searchList: [],
    // 当前位置的经纬度
    latitude: '',
    longitude: '',
  },
  //调用百度api收索功能实时监听input的输入 
  bindKeyInput: function (e) {
    var that = this;
    var BMap = new bmap.BMapWX({
      ak: 'ybwFsz2qPkwHTqhQWuBHLs9dCXBRXC9G'
    });
    var fail = function (data) {
      console.log('data fail')

    };
    var success = function (data) {
      console.log(data)
      var searchList = [],
        posTxt = "",
        id = "",
        latitude = '',
        longitude = '',
        posImg = "../../images/logo.png";
      for (var i = 0; i < data.result.length; i++) {
        posTxt = data.result[i].name
        id = data.result[i].uid
        latitude = data.result[i].location.lat
        longitude = data.result[i].location.lng
        searchList[i] = {
          posTxt,
          id,
          posImg,
          latitude,
          longitude,
        }
      }
      // console.log(searchList)
      that.setData({
        //先在定义变量存数据，然后再一起放入setData里面去，就上面几行的位置
        searchList: searchList,
      });
    }
    // 发起suggestion检索请求 
    BMap.suggestion({
      query: e.detail.value,
      region: '北京',
      city_limit: true,
      fail: fail,
      success: success
    });
  },

  //show地图中当前位置
  gotoMap: function (e) {
    console.log(e)
    var lat = Number(e.currentTarget.dataset.latitude);
    var lon = Number(e.currentTarget.dataset.longitude);
    wx.openLocation({
      type: 'gcj02',
      latitude: lat,
      longitude: lon,
      scale: 28
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 先加载当前位置(经纬度)
  onLoad: function (options) {
    var that = this
    var my_lat = '',
      my_lng = ''
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        my_lat = (res.latitude).toFixed(5)
        my_lng = (res.longitude).toFixed(5)
        that.setData({
          latitude: my_lat,
          longitude: my_lng,
        })
      },
    })
  },

  


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})